from django.shortcuts import render,HttpResponse
from django.views.decorators.csrf import csrf_exempt
from cinema_manage import models
import json
# Create your views here.

@csrf_exempt
def login(request):
    if request.method =="POST":
        account = request.POST.get('account')
        password = request.POST.get('password')
        #验证账户是否存在
        try:
            manager_info = models.manager.objects.get(account = account)
        except:
            msg = {"msg":"管理员未找到"}
            res = HttpResponse(json.dumps(msg),content_type="appliaction/json")
            return res
        #验证密码
        if(manager_info.password != password):#密码不正确
            msg = {"msg":"密码不正确"}
            res = HttpResponse(json.dumps(msg),content_type="application/json")
            return res
        else:#密码正确
            if manager_info.permissions == 0:#网站管理员
                href = "webmanager/"
            elif manager_info.permissions == 1:#片场管理员
                href = "cinemamanager/"
            elif manager_info.permissions == 2:#前台管理员
                href = "reception/"
            #设置session,cookie
            msg = {"msg":"登录成功","href":href}
            res = HttpResponse(json.dumps(msg),content_type="application/json")
            request.session["is_login"] = True
            request.session["type"] = manager_info.permissions
            res.set_cookie("is_login",True)
            return res
    else:
        return render(request,'man_login.html')


#网站管理员页面
def webmanager(request):
    return render(request,'webmanager.html')

#影院场次管理员
def cinema_boss(request):
    return render(request,'cinema_boss.html')

#影院前台
def cinema_reception(request):
    return render(request,'cinema_reception.html')